<script setup>
import { Icon } from '@iconify/vue';
import { useI18n } from 'vue-i18n';
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow';

// Avoid calling Tauri APIs when running in a regular browser (Vite dev/preview)
let appWindow = null;
const isTauri = typeof window !== 'undefined' && !!window.__TAURI_INTERNALS__;
if (isTauri) {
  import('@tauri-apps/api/webviewWindow')
    .then(mod => {
      try { appWindow = mod.getCurrentWebviewWindow(); } catch {}
    })
    .catch(() => {});
}

const { t } = useI18n();

async function minimize() { try { await appWindow?.minimize(); } catch {} }
async function toggleMaximize() {
  try {
    const maximized = await appWindow?.isMaximized();
    if (maximized) await appWindow?.unmaximize(); else await appWindow?.maximize();
  } catch {}
}
async function closeWin() { try { await appWindow?.close(); } catch {} }
</script>

<template>
  <div class="flex items-center gap-1">
    <button class="h-7 w-7 inline-flex items-center justify-center rounded-md hover:bg-slate-100 dark:hover:bg-neutral-800" :title="t('app.window_minimize')" @click.stop="minimize">
      <Icon icon="codicon:chrome-minimize" width="14" height="14" />
    </button>
    <button class="h-7 w-7 inline-flex items-center justify-center rounded-md hover:bg-slate-100 dark:hover:bg-neutral-800" :title="t('app.window_maximize')" @click.stop="toggleMaximize">
      <Icon icon="codicon:chrome-maximize" width="14" height="14" />
    </button>
    <button class="h-7 w-7 inline-flex items-center justify-center rounded-md hover:bg-red-500/10 dark:hover:bg-red-500/10" :title="t('app.window_close')" @click.stop="closeWin">
      <Icon icon="codicon:chrome-close" width="14" height="14" />
    </button>
  </div>
</template>


